package com.friend.joey.applicationfriend.Adapters;

import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.friend.joey.applicationfriend.Classes.Msgtalk;
import com.friend.joey.applicationfriend.R;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.baidu.location.h.j.O;

/**
 * Created by Joey on 2017/4/24.
 */

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    public static final int COMPUTER=0;
    public static final int CHILD=1;
    public static final int OLDMAN=2;
    public static final int FRIEND=3;


    private int type;
    private ArrayList<Msgtalk> MsgList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout msglayout;
        TextView Msgtxt;
        CircleImageView Imageleft;
        CircleImageView Imageright;
        public ViewHolder(View view){
            super(view);
            msglayout = (LinearLayout)view.findViewById(R.id.msg_txtlayout);
            Msgtxt=(TextView)view.findViewById(R.id.txt_msg);
            Imageleft=(CircleImageView)view.findViewById(R.id.button_msgcomputer);
            Imageright=(CircleImageView)view.findViewById(R.id.button_msguser);
        }
    }

    public MsgAdapter(ArrayList<Msgtalk> msgList,int type){
        MsgList = msgList;
        this.type=type;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ftmsg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msgtalk msg = MsgList.get(position);
        if(type==COMPUTER) {
            if (msg.getType() == Msgtalk.TYPE_RECEIVED) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.LEFT);
                holder.msglayout.setGravity(Gravity.LEFT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkgreen3);
                holder.Imageleft.setVisibility(View.VISIBLE);
                holder.Imageright.setVisibility(View.INVISIBLE);
                holder.Imageleft.setImageResource(R.drawable.computer);
            } else if (msg.getType() == Msgtalk.TYPE_SENT) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.RIGHT);
                holder.msglayout.setGravity(Gravity.RIGHT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkblue3);
                holder.Imageleft.setVisibility(View.INVISIBLE);
                holder.Imageright.setVisibility(View.VISIBLE);
                holder.Imageright.setImageResource(R.drawable.oldman);
            }
        }
        if(type==OLDMAN){
            if (msg.getType() == Msgtalk.TYPE_OLDMAN) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.LEFT);
                holder.msglayout.setGravity(Gravity.LEFT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkgreen3);
                holder.Imageleft.setVisibility(View.VISIBLE);
                holder.Imageright.setVisibility(View.INVISIBLE);
                holder.Imageleft.setImageResource(R.drawable.oldman);
            } else if (msg.getType() == Msgtalk.TYPE_CHILD) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.RIGHT);
                holder.msglayout.setGravity(Gravity.RIGHT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkblue3);
                holder.Imageleft.setVisibility(View.INVISIBLE);
                holder.Imageright.setVisibility(View.VISIBLE);
                holder.Imageright.setImageResource(R.drawable.child);
            }
        }
        if(type==CHILD){
            if (msg.getType() == Msgtalk.TYPE_CHILD) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.LEFT);
                holder.msglayout.setGravity(Gravity.LEFT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkgreen3);
                holder.Imageleft.setVisibility(View.VISIBLE);
                holder.Imageright.setVisibility(View.INVISIBLE);
                holder.Imageleft.setImageResource(R.drawable.child);
            } else if (msg.getType() == Msgtalk.TYPE_OLDMAN) {
                holder.Msgtxt.setText(msg.getContent());
                holder.Msgtxt.setGravity(Gravity.RIGHT);
                holder.msglayout.setGravity(Gravity.RIGHT);
                holder.Msgtxt.setBackgroundResource(R.drawable.talkblue3);
                holder.Imageleft.setVisibility(View.INVISIBLE);
                holder.Imageright.setVisibility(View.VISIBLE);
                holder.Imageright.setImageResource(R.drawable.oldman);
            }
        }
    }

    @Override
    public int getItemCount() {
        return MsgList.size();
    }
}
